#' <Add Title>
#'
#' <Add Description>
#'
#' @param data
#' @param zoomRatio 缩放比例
#' @param width
#' @param height
#' @param elementId
#' @param axis.y.text  relative absolute
#'
#' @import htmlwidgets
#'
#' @export
peakD3 <- function(data,
                   zoomRatio=50,
                   axisY="relative",
                   title = "",
                   titleX = "M/Z",
                   titleY = "",
                   ifLabel = T,
                   bColor = "#d35151",
                   yColor= "#3434f3",
                   otherColor="#d8d8d8",
                   showControls = F,
                   ggplotSvg = NULL,
                   width = NULL, height = NULL, elementId = NULL) {

  # forward options using x
  x = list(
    data=data,
    zoomRatio=zoomRatio,
    axisY = axisY,
    title = title,
    titleX = titleX,
    titleY = titleY,
    ifLabel = ifLabel,
    bColor = bColor,
    yColor = yColor,
    otherColor = otherColor,
    showControls=showControls,
    ggplotSvg = ggplotSvg

  )

  # create widget
  htmlwidgets::createWidget(
    name = 'peakD3',
    x,
    width = width,
    height = height,
    package = 'D3Widget',
    elementId = elementId
  )
}

#' Shiny bindings for peakD3
#'
#' Output and render functions for using peakD3 within Shiny
#' applications and interactive Rmd documents.
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#'   \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#'   string and have \code{'px'} appended.
#' @param expr An expression that generates a peakD3
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
#'   is useful if you want to save an expression in a variable.
#'
#' @name peakD3-shiny
#'
#' @export
peakD3Output <- function(outputId, width = '100%', height = '400px'){
  htmlwidgets::shinyWidgetOutput(outputId, 'peakD3', width, height, package = 'D3Widget')
}

#' @rdname peakD3-shiny
#' @export
renderPeakD3 <- function(expr, env = parent.frame(), quoted = FALSE) {
  if (!quoted) { expr <- substitute(expr) } # force quoted
  htmlwidgets::shinyRenderWidget(expr, peakD3Output, env, quoted = TRUE)
}
